package com.zb.jnlxc.action;

import java.io.*;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONObject;
import com.zb.jnlxc.form.MiniPageReq;
import com.zb.jnlxc.form.MiniPageRsp;
import com.zb.jnlxc.model.Admin;
import com.zb.jnlxc.model.Scheme;
import com.zb.jnlxc.service.SchemeService;

/**
 * Created with IntelliJ IDEA.
 * User: root
 * Date: 12-12-29
 * Time: 下午3:33
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/SchemeAction")
@SessionAttributes(value = { "user" })
public class SchemeAction {
    Logger         logger = LoggerFactory.getLogger(SchemeAction.class);
    @Resource
    SchemeService  schemeService;

    @ResponseBody
    @RequestMapping("/addUpdateScheme")
    public Scheme addUpdateScheme(String schemeData, String uniqid,
                                  @ModelAttribute("user") Admin user, String operate) {
        schemeData = schemeData.replace("\"payType\":\"\",", "");
        logger.info("schemeData={}", schemeData);
        Scheme scheme = JSONObject.parseObject(schemeData, Scheme.class);
        operate = operate == null ? "addScheme" : operate;
        if (operate.equals("addSchemeStartSchemeFlow"))
            return schemeService.addSchemeStartSchemeFlow(scheme, uniqid, user);
        else if (operate.equals("addSchemeWithOutFlow"))
            return schemeService.addSchemeWithOutFlow(scheme, uniqid, user);
        else if (operate.equals("editSchemeWithOutFlow"))
            return schemeService.editSchemeWithOutFlow(scheme, uniqid);
        else if (operate.equals("editSchemeStartSchemeFlow"))
            return schemeService.editSchemeStartSchemeFlow(scheme, uniqid);
        else if (operate.equals("editSchemeStartEditMouldFlow"))
            return schemeService.editSchemeStartEditMouldFlow(scheme, uniqid);
        else
            return null;
    }

    @ResponseBody
    @RequestMapping("/moulderLoock")
    public void moulderLoock(String taskId, Admin repairer, @ModelAttribute("user") Admin user) {
        schemeService.moulderLoock(taskId, repairer, user);
    }

    @ResponseBody
    @RequestMapping("/repairerEditMould")
    public void repairerEditMould(String taskId, @ModelAttribute("user") Admin user) {
        schemeService.repairerEditMould(taskId, user);
    }

    /**
     * 查看图纸
     * @param page
     * @return
     */
    @ResponseBody
    @RequestMapping("/loadScheme")
    public MiniPageRsp loadScheme(MiniPageReq page, HttpServletRequest request) {
        page.setRequest(request);
        schemeService.LoadScheme(page);
        return new MiniPageRsp(page.getResultData(), page.getTotalClum());
    }

    @RequestMapping(value = "/mapUpload")
    @ResponseBody
    public Map mapUpload(@RequestParam("file") MultipartFile file) throws IOException {
        if (!file.isEmpty()) {
            return schemeService.changeToTempFile(file);
        } else {
            return null;
        }
    }

    @RequestMapping(value = "/downTempImg")
    public void downTempImg(String uniqid, HttpServletResponse response) {
        schemeService.downTempImg(uniqid, response);
    }

    @RequestMapping(value = "/downImg")
    public void downImg(Integer schemeId, HttpServletResponse response) {
        schemeService.downImg(schemeId, response);
    }

    @RequestMapping(value = "/downImgByCode")
    public void downImgByCode(String code, HttpServletResponse response) {
        schemeService.downImgByCode(code,response);
    }

    @RequestMapping(value = "/downDwg")
    public void downDwg(Integer schemeId, HttpServletResponse response) {
        schemeService.downDwg(schemeId,response);
    }

    @ResponseBody
    @RequestMapping(value = "/getScheme")
    public Scheme getScheme(Integer schemeId) {
        Scheme scheme = schemeService.getById(schemeId);
        return scheme;
    }

    @ResponseBody
    @RequestMapping(value = "/getSchemeInfoByTask")
    public Scheme getSchemeInfoByTask(String taskId) {
        return schemeService.getSchemeInfoByTask(taskId);
    }

    @ResponseBody
    @RequestMapping("/deleteScheme")
    public void deleteScheme(Integer schemeId) {
        schemeService.deleteScheme(schemeId);
    }

    @ResponseBody
    @RequestMapping("/getAllProductRecordDetail")
    public MiniPageRsp getAllProductRecordDetail(Integer schemeId) {
        List l = schemeService.getAllProductRecordDetail(schemeId);
        return new MiniPageRsp(l, l.size());
    }

    @ResponseBody
    @RequestMapping("/osscopy")
    public void osscopy(String dir,final String des,int tcount) throws Exception {
        ExecutorService pool = Executors.newFixedThreadPool(tcount);
        File file = new File(dir);
        File[] files = file.listFiles();
        int a=0;
        for(final File f :files){
            final int i = a++;
            pool.execute(new Runnable() {
                @Override
                public void run() {
                    logger.info("i = {} filename={}", i, f.getName());
                    schemeService.uploadFile(f,des);
                }
            });
        }
    }

}
